package sjtu.ist.warehouse4ws.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.model.Oplog;
import sjtu.ist.warehouse4ws.service.OplogService;
import sjtu.ist.warehouse4ws.tool.BaseAction;
import sjtu.ist.warehouse4ws.tool.CnZipFileDemo;

public class FileDownloadAction extends   BaseAction{

	private String groupName;
	private String version;
	private RepositoryMoudl repositoryMoudlService;	
	private OplogService oplogService;
	
	public void setOplogService(OplogService oplogService) {
		this.oplogService = oplogService;
	}

	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService) {
		this.repositoryMoudlService = repositoryMoudlService;
	}
	
	private InputStream fileInputStream;
	 

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public String execute() throws Exception {
		Subject cuser=SecurityUtils.getSubject();
		String companyId=cuser.getSession().getAttribute("companyId").toString();
		//repositoryMoudlService.get
		
		String filePath=repositoryMoudlService.getNodePath(companyId,groupName,version);
		System.out.println("=====filePath:"+filePath);
//	    fileInputStream=repositoryMoudlService.getModelXmlFile(path).get(0);
		//内容库的文件，打包到这个文件
		String docpath = getSession().getServletContext().getRealPath("/")+"file\\";  
		//为了方便下载，把这个包压缩
		String zippath=getSession().getServletContext().getRealPath("/")+"file.zip";

		CnZipFileDemo cnzip=new CnZipFileDemo();
		repositoryMoudlService.getModelXmlFile(filePath, docpath);
		cnzip.zip(docpath, zippath,"GBK");//docpath+b.zip生成压缩的文件放在指定位置
		fileInputStream = new FileInputStream(new File(zippath));
		
		//插入记录
		Oplog oplog=new Oplog();
		oplog.setGroupId(groupName);
		oplog.setUser(cuser.getSession().getAttribute("username").toString());
		oplog.setOperation(Oplog.download);
		oplog.setDiscription("下载模型");
		oplogService.insertOplog(oplog);
		
	    return SUCCESS;
	}

	/**
	 * @param groupId the groupId to set
	 */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}
	
}
